package com.ljm.util;/**
 * @author： li
 * @date： 2022-05-05 19:58
 * @version 1.0
 */

import com.ljm.bin.imageUrl;
import com.ljm.bin.wxuser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import sun.security.pkcs11.Secmod;

import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.DuplicateFormatFlagsException;
import java.util.List;

/**
 * @author： ljm
 * @date： 2022-05-05 19:58
 * @version 1.0
 */
public class ImageUrlUtil {
    static String[] ua = {"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36 OPR/37.0.2178.32",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
            "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
            "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
            "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
            "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0)",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 BIDUBrowser/8.3 Safari/537.36",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.12150.8 Safari/537.36",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 TheWorld 7",
            "Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/60.0",
            "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
    };
    public static int updateUrl(int n) throws IOException, SQLException {
        Connection con = DBUtil1.getConnection();
        String sql= "select * from images where date=?";
        String sql1 = "insert into images values(?,?,?)";
        int flag = 0;
        for(int i=1;i<=n;i++){
        Document document = Jsoup.connect("https://bing.ioliu.cn/?p="+i)
                .proxy("61.153.251.150",22222)
                .userAgent(ua[15])//                referrer("https://cn.bing.com/").
                .get();
        Elements elements = document.select("a.mark");
            for(Element element:elements) {
//                System.out.println(element.toString());
                String href = element.attr("href");
                String newURL = "https://bing.ioliu.cn/" + href + "";
                Document document1 = Jsoup.connect(newURL)
                        .proxy("61.153.251.150",22222)
                        .userAgent(ua[15])
                        //                        .referrer("https://bing.ioliu.cn/")
                        .get();
                String title = document1.select("p.title").text();
                title=title.replaceAll("\\(.*\\)","");
                String[] content =document1.select("em.t").text().split(" ");
                String imageSrc = document1.select("img.target.progressive__img.progressive--not-loaded").attr("data-progressive");
                System.out.println(content[4]);
                PreparedStatement ps=con.prepareStatement(sql),ps1=con.prepareStatement(sql1);
                ps.setString(1,content[4]);
                ps1.setString(1,content[4]);
                ps1.setString(2,title);
                ps1.setString(3,imageSrc);
                if(!ps.execute())
                    flag=ps1.executeUpdate();
//                DBUtil1.close(ps,ps1);
            }
        }
            DBUtil1.close(con);
            return flag;
    }
    public static void getUrl() throws IOException {
        int len =ua.length;
        Document document = Jsoup.connect("https://bing.ioliu.cn/?p=1")
//                .proxy("61.216.185.88",60808)
                .userAgent(ua[15])//ua[(int)(Math.random()*(len-1))]
                .get();
//        Document document =Jsoup.parse(new URL("https://bing.ioliu.cn/?p=1"),30000);
        Elements elements = document.select("a.mark");
        for(Element element:elements) {
//                System.out.println(element.toString());
            String href = element.attr("href");
            String newURL = "https://bing.ioliu.cn/" + href + "";
            Document document1 = Jsoup.connect(newURL)
//                    .proxy("61.216.185.88",60808)
                    .userAgent(ua[15])
                    .get();
//            Document document1 = Jsoup.parse(new URL(newURL),30000);
            String title = document1.select("p.title").text();
            title=title.replaceAll("\\(.*\\)","");
            String[] content =document1.select("em.t").text().split(" ");
            String imageSrc = document1.select("img.target.progressive__img.progressive--not-loaded").attr("data-progressive");
            System.out.println(content[4]);
            System.out.println(title);
            System.out.println(imageSrc);
        }
    }
    public static List<imageUrl> result(int k) throws SQLException {
        //0.创建集合
        List<imageUrl> user=new ArrayList<>();
        //1，获取连接.
        Connection con = DBUtil1.getConnection();
// 2执行sqL语句
//2,1创建sqL语句
        String sql1 = "select * from images where class=?";
        PreparedStatement ps = con.prepareStatement(sql1);
        ps.setInt(1,k);
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            String date=rs.getString("date");
            String title=rs.getString("title");
            String url=rs.getString("url");
            //控制台输出
          /*  System.out.println("id："+id+"，username："+username+
                    "，password："+password+"，state："+state);*/
            imageUrl iu = new imageUrl(title,url,date);
            user.add(iu);
        }
        DBUtil1.close(ps,con);
        return user;
    }
    //https://konachan.net
    public static void getImages1() throws IOException, SQLException {
        Connection con = DBUtil1.getConnection();
        String sql="insert into images value(?,?,?)";
        int len =ua.length;
        Document document = Jsoup.connect("https://konachan.net/post?page=5")
                .userAgent(ua[(int)(Math.random()*(len-1))])
                .get();
//        System.out.println(document.toString());
        Elements elements = document.select("a.thumb");
        for(Element element:elements){
            String href = element.attr("href");
            String newUrl ="https://konachan.net"+href+"";
            Document document1 =Jsoup.connect(newUrl)
                    .userAgent(ua[(int)(Math.random()*(len-1))])
                    .get();
            String date = LocalDate.now().toString();
            String title = "From https://konachan.net";
            System.out.println(title);
            String imageUrls =document1.select("img.image").attr("src");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1,date);
            ps.setString(2,title);
            ps.setString(3,imageUrls);
            ps.executeUpdate();
//            System.out.println(imageUrls);
        }
        DBUtil1.close(con);
    }

    static String[] urls={
            //精选壁纸 0
            "https://wall.alphacoders.com/featured.php?lang=Chinese",
            //原创壁纸 1
            "https://wall.alphacoders.com/by_creator.php?lang=Chinese",
            //随机壁纸 2
            "https://wall.alphacoders.com/random.php?lang=Chinese",
            //自然壁纸 3
            "https://wall.alphacoders.com/by_category.php?id=10&name=%E8%87%AA%E7%84%B6+%E5%A3%81%E7%BA%B8&lang=Chinese",
            //科幻壁纸 4
            "https://wall.alphacoders.com/by_category.php?id=27&name=%E7%A7%91%E5%B9%BB+%E5%A3%81%E7%BA%B8&lang=Chinese",
            //艺术壁纸 5
            "https://wall.alphacoders.com/by_category.php?id=4&name=%E8%89%BA%E6%9C%AF+%E5%A3%81%E7%BA%B8&lang=Chinese",
            //风景壁纸 6
            "https://wall.alphacoders.com/search.php?search=landscape&lang=Chinese",
            //节日壁纸 7
            "https://wall.alphacoders.com/by_category.php?id=15&name=%E8%8A%82%E6%97%A5+%E5%A3%81%E7%BA%B8&lang=Chinese",
            //技术壁纸 8
            "https://wall.alphacoders.com/by_category.php?id=30&name=%E6%8A%80%E6%9C%AF+%E5%A3%81%E7%BA%B8&lang=Chinese",
            //食物壁纸 9
            "https://wall.alphacoders.com/by_category.php?id=12&name=%E9%A3%9F%E7%89%A9+%E5%A3%81%E7%BA%B8&lang=Chinese",
            //手机壁纸 10
            "https://mobile.alphacoders.com/by-resolution/60/1440x2960-Wallpapers"
    };
    public static void getImages2() throws IOException, SQLException, InterruptedException {
        Connection con = DBUtil1.getConnection();
        String sql="insert ignore into images value(?,?,?,?)";
        int len =ua.length;
        String date = LocalDate.now().toString();
//        String title = "From https://wall.alphacoders.com/featured.php?lang=Chinese";
        for(int i=10;i<=10;i++){
        Document document = Jsoup.connect(urls[i])
                .userAgent(ua[(int)(Math.random()*(len-1))])
                .get();
//        System.out.println(document.toString());
        Elements elements = document.select("img.img-responsive");
        for(Element element:elements){
//            System.out.println(element.toString());
            String url =element.attr("src");
            String title1 =element.attr("alt");
            String title =title1.replaceAll("[壁纸|高清壁纸|桌面背景]","");
            title=title.trim();
            System.out.println(title);
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1,date);
            ps.setString(2,title);
            ps.setString(3,url);
            ps.setInt(4,i);
            ps.executeUpdate();
//            System.out.println(imageUrls);
            }
        Thread.sleep(2000);
        }
        DBUtil1.close(con);
    }
    public static void main(String[] args) throws IOException, SQLException, InterruptedException {
            getImages2();
//        getImages1();
//        List<imageUrl> rs;
//        rs = result();
//        for(imageUrl i:rs){
//            System.out.println(i.getDate()+" "+i.getTitle()+" "+i.getUrl());
//        }
    }
}
